package com.lovo.system.controller;

import com.lovo.system.dto.PurchaseDto;
import com.lovo.system.service.IPurchaseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/purchase")
public class PurchaseController {

    @Autowired
    IPurchaseService purchaseService;

    @RequestMapping("/info")
    public List<PurchaseDto> info(HttpServletRequest request) {
        return purchaseService.info((Integer) request.getSession().getAttribute("userId"));
    }

    @RequestMapping("/buyer")
    public String buyer(HttpServletRequest request){
        return (String) request.getSession().getAttribute("realName");
    }

    @RequestMapping("/add")
    public void addPurchase(@RequestBody PurchaseDto purchaseDto,HttpServletRequest request) {
        purchaseDto.setUserId((Integer) request.getSession().getAttribute("userId"));
        if (purchaseService.selectRepeat(purchaseDto) == 1) {
            purchaseService.insertPurchase(purchaseDto);
        }
    }

    @RequestMapping("/delete")
    public String deletePurchase(int id) {
        return purchaseService.deletePurchase(id);
    }

    @RequestMapping("/changeNumber")
    public void changeNumber(@RequestBody PurchaseDto purchaseDto) {
        purchaseService.updatePurchase(purchaseDto);
    }

    @RequestMapping("/deleteAll")
    public void deleteAll(HttpServletRequest request) {
        purchaseService.deleteAll((Integer) request.getSession().getAttribute("userId"));
    }

}
